package backstage.app.util;

import java.util.Date;

import org.springframework.stereotype.Service;

import io.jsonwebtoken.Claims;
 
import io.jsonwebtoken.Jwts;
 
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
 
@Data
@Service
public class JwtTokenUtil {
	 
	/**
	 * 创建token
	 * 注：如果是根据可变的唯一值来生成，唯一值变化时，需重新生成token
	 * @param username
	 * @param isRememberMe
	 * @return
	 */
  
    /*
     * 根据身份ID标识，生成Token
     */
    public String getToken (String identityId){
        Date nowDate = new Date();
        //过期时间
        Date expireDate = new Date(nowDate.getTime() + expire * 60000);
        return Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setSubject(identityId)
                .setIssuedAt(nowDate)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
    /** 
     获取 Token 中注册信息
     */
    public Claims getTokenClaim (String token) {
        try {
            return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /*
     * Token 是否过期验证
     */
    public boolean isTokenExpired (Date expirationTime) {
        return expirationTime.before(new Date());
    }
    private String secret="wang-wei-quan";
    private long expire=100;
    private String header="jwt";
}
